package MainPage;



//import javax.swing.*;
//import javax.swing.event.*;

import java.awt.Color;
import java.awt.Component;
import java.util.GregorianCalendar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//import javax.swing.table.*;
//import javax.swing.JTable;
//import java.awt.*;
//import java.awt.event.*;
//import java.util.*;
//import java.awt.GridLayout;
/**
 *
 * @author EK
 */
public class calendarGUI extends javax.swing.JFrame implements ActionListener {

    int realYear, realMonth, realDay, currentYear, currentMonth;
    String day,month,year;
    public static String combineDate=null,combineDateOut=null;

    /** Creates new form calendarGUI */
    public calendarGUI() {
        initComponents();
        init();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(700,500);
        setLocationRelativeTo(null);
        refreshCalendar(realMonth, realYear);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        int realYear, realMonth, realDay, currentYear, currentMonth;

        GregorianCalendar cal = new GregorianCalendar(); //Create calendar
        realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); //Get day
        realMonth = cal.get(GregorianCalendar.MONTH); //Get month
        realYear = cal.get(GregorianCalendar.YEAR); //Get year
        currentMonth = realMonth; //Match month and year
        currentYear = realYear;
        yearComboBox = new javax.swing.JComboBox();
        lblMonth = new javax.swing.JLabel();
        lblYear = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        lblPrev = new javax.swing.JLabel();
        lblPrev1 = new javax.swing.JLabel();
        lblNext = new javax.swing.JLabel();
        lblNext1 = new javax.swing.JLabel();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setCellSelectionEnabled(true);
        jTable1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jTable1.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jTable1.setFillsViewportHeight(true);
        jTable1.setMaximumSize(new java.awt.Dimension(525, 230));
        jTable1.setMinimumSize(new java.awt.Dimension(525, 230));
        jTable1.setPreferredSize(new java.awt.Dimension(525, 230));
        jTable1.setRowHeight(40);
        jTable1.setRowMargin(2);
        jTable1.setSelectionBackground(new java.awt.Color(255, 153, 0));
        jTable1.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.setSurrendersFocusOnKeystroke(true);
        jTable1.getTableHeader().setResizingAllowed(false);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.setUpdateSelectionOnSort(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);
        jTable1.getColumnModel().getColumn(4).setResizable(false);
        jTable1.getColumnModel().getColumn(5).setResizable(false);
        jTable1.getColumnModel().getColumn(6).setResizable(false);

        jScrollPane1.setBounds(150, 70, 410, 260);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        yearComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yearComboBoxActionPerformed(evt);
            }
        });
        yearComboBox.setBounds(360, 360, 80, 20);
        jLayeredPane1.add(yearComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);
        for (int i=realYear-100; i<=realYear+100; i++)
        {
            yearComboBox.addItem(String.valueOf(i));
        }

        lblMonth.setFont(new java.awt.Font("Meiryo", 3, 24)); // NOI18N
        lblMonth.setForeground(new java.awt.Color(255, 0, 0));
        lblMonth.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMonth.setText("January");
        lblMonth.setBounds(280, 30, 150, 28);
        jLayeredPane1.add(lblMonth, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblYear.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblYear.setForeground(new java.awt.Color(255, 0, 0));
        lblYear.setText("Change Year :");
        lblYear.setBounds(265, 360, 100, 20);
        jLayeredPane1.add(lblYear, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("Store New Event");
        jButton1.addActionListener(this);
        jButton1.setBounds(380, 420, 170, 23);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setText("Back");
        jButton2.addActionListener(this);
        jButton2.setBounds(230, 420, 120, 23);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        

        lblPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/prev.png"))); // NOI18N
        lblPrev.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblPrevMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblPrevMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblPrevMouseExited(evt);
            }
        });
        lblPrev.setBounds(60, 170, 70, 70);
        jLayeredPane1.add(lblPrev, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblPrev1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource//prevGlow.png"))); // NOI18N
        lblPrev1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblPrev1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblPrev1MouseEntered(evt);
            }
        });
        lblPrev1.setBounds(60, 160, 80, 90);
        jLayeredPane1.add(lblPrev1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lblNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/next.png"))); // NOI18N
        lblNext.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblNextMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblNextMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblNextMouseExited(evt);
            }
        });
        lblNext.setBounds(570, 170, 70, 81);
        jLayeredPane1.add(lblNext, javax.swing.JLayeredPane.DEFAULT_LAYER);
        lblNext.getAccessibleContext().setAccessibleName("lblNext");

        lblNext1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/nextGlow.png"))); // NOI18N
        lblNext1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblNext1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblNext1MouseEntered(evt);
            }
        });
        lblNext1.setBounds(570, 170, 80, 80);
        jLayeredPane1.add(lblNext1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        lblNext1.getAccessibleContext().setAccessibleName("lblNext1");

        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/panel2.jpg"))); // NOI18N
        background.setPreferredSize(new java.awt.Dimension(700, 500));
        background.setBounds(0, 0, 700, 500);
        jLayeredPane1.add(background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 699, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

private void yearComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
    if (yearComboBox.getSelectedItem() != null) {
        String b = yearComboBox.getSelectedItem().toString();
        currentYear = Integer.parseInt(b);
        refreshCalendar(currentMonth, currentYear);
    }
}                                            

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    //new Color(0, 220, 255)
    //jTable1.setSelectionBackground(new Color(0, 220, 255));
	day=jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn()).toString();
	month=getmonth();
	year=(String)yearComboBox.getSelectedItem();
	combineDate=day+"-"+month+"-"+year;
	combineDateOut=day+"-"+Integer.toString(currentMonth+1)+"-"+year;
	System.out.println(combineDate);
}                                    

public String getmonth()
{
	String configMonth = null;
	switch(currentMonth)
	{
	    case 0:
	    	configMonth="JAN";
	    	break;
	    case 1:
	    	configMonth="FEB";
	    	break;
	    case 2:
	    	configMonth="MAR";
	    	break;
	    case 3:
	    	configMonth="APR";
	    	break;
	    case 4:
	    	configMonth="MAY";
	    	break;
	    case 5:
	    	configMonth="JUN";
	    	break;
	    case 6:
	    	configMonth="JUL";
	    	break;
	    case 7:
	    	configMonth="AUG";
	    	break;
	    case 8:
	    	configMonth="SEP";
	    	break;
	    case 9:
	    	configMonth="OCT";
	    	break;
	    case 10:
	    	configMonth="NOV";
	    	break;
	    case 11:
	    	configMonth="DEC";
	    	break;
	}
	return configMonth; 
}

private void lblNextMouseClicked(java.awt.event.MouseEvent evt) {
    if (currentMonth == 11 && currentYear >= realYear + 100) {
    lblNext1.setVisible(false);
    lblNext.setVisible(false);
    }
    else
    {
    if (currentMonth == 11) { //Foward one year
        currentMonth = 0;
        currentYear += 1;
    } else { //Foward one month
        currentMonth += 1;
    }
    refreshCalendar(currentMonth, currentYear);
    }
    
}

private void lblPrevMouseClicked(java.awt.event.MouseEvent evt) {
    if (currentMonth == 0 && currentYear <= realYear - 10) {
    lblPrev1.setVisible(false);
    lblPrev.setVisible(false);
    }
    else
    {
    if (currentMonth == 0) { //Back one year
        currentMonth = 11;
        currentYear -= 1;
    } else { //Back one month
        currentMonth -= 1;
    }
    refreshCalendar(currentMonth, currentYear);
    }
    
}

private void lblPrevMouseEntered(java.awt.event.MouseEvent evt) {
    if (currentMonth == 0 && currentYear <= realYear - 10) {
    lblPrev1.setVisible(false);
    lblPrev.setVisible(false);
    }
    else
    {
    lblPrev.setVisible(false);
    lblPrev1.setVisible(true);
    }
}

private void lblPrev1MouseEntered(java.awt.event.MouseEvent evt) {
    //lblPrev.setVisible(true);
    //lblPrev1.setVisible(false);
}

private void lblNext1MouseClicked(java.awt.event.MouseEvent evt) {

    if (currentMonth == 11 && currentYear >= realYear + 100) {
    lblNext1.setVisible(false);
    lblNext.setVisible(false);
    }
    else
    {
    if (currentMonth == 11) { //Foward one year
        currentMonth = 0;
        currentYear += 1;
    } else { //Foward one month
        currentMonth += 1;
    }
    refreshCalendar(currentMonth, currentYear);
    }
}

private void lblPrev1MouseClicked(java.awt.event.MouseEvent evt) {
    if (currentMonth == 0 && currentYear <= realYear - 10) {
    lblPrev1.setVisible(false);
    lblPrev.setVisible(false);
    }
    else
    {
    if (currentMonth == 0) { //Back one year
        currentMonth = 11;
        currentYear -= 1;
    } else { //Back one month
        currentMonth -= 1;
    }
    refreshCalendar(currentMonth, currentYear);
    }
    
}

private void lblNext1MouseEntered(java.awt.event.MouseEvent evt) {
    //lblNext.setVisible(true);
    //lblNext1.setVisible(false);
}

private void lblNextMouseEntered(java.awt.event.MouseEvent evt) {
    if (currentMonth == 11 && currentYear >= realYear + 100) {
    lblNext.setVisible(false);
    lblNext1.setVisible(false);
    }
    else
    {
    lblNext.setVisible(false);
    lblNext1.setVisible(true);
    }
}

private void lblNextMouseExited(java.awt.event.MouseEvent evt) {
    if (currentMonth == 11 && currentYear >= realYear + 100) {
    lblNext1.setVisible(false);
    lblNext.setVisible(false);
    }
    else
    {
    lblNext.setVisible(true);
    lblNext1.setVisible(false);
    }
}

private void lblPrevMouseExited(java.awt.event.MouseEvent evt) {
    if (currentMonth == 0 && currentYear <= realYear - 10) {
    lblPrev1.setVisible(false);
    lblPrev.setVisible(false);
    }
    else
    {
    lblPrev.setVisible(true);
    lblPrev1.setVisible(false);
    }

}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new calendarGUI().setVisible(true);
            }
        });
    }

    public void init()
    {

        GregorianCalendar cal = new GregorianCalendar(); //Create calendar
        realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); //Get day
        realMonth = cal.get(GregorianCalendar.MONTH); //Get month
        realYear = cal.get(GregorianCalendar.YEAR); //Get year
        currentMonth = realMonth; //Match month and year
        currentYear = realYear;
        refreshCalendar(currentMonth, currentYear);
    }

    public void refreshCalendar(int month, int year) {
        //Variables

        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        int nod, som; //Number Of Days, Start Of Month

        
            lblPrev.setVisible(true);
            lblNext.setVisible(true);
        /**
        if (month == 0 && year <= realYear - 10) {
            lblPrev1.setVisible(false);
            lblPrev.setVisible(false);
        } //Too early
        if (month == 11 && year >= realYear + 100) {
            lblNext.setVisible(false);
            lblNext1.setVisible(false);            
        } //Too late**/
        lblMonth.setText(months[month]); //Refresh the month label (at the top)
        yearComboBox.setSelectedItem(String.valueOf(year)); //Select the correct year in the combo box

        //Clear table
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                jTable1.setValueAt(null, i, j);
            }
        }

        //Get first day of month and number of days
        GregorianCalendar cal = new GregorianCalendar(year, month, 1);
        nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
        som = cal.get(GregorianCalendar.DAY_OF_WEEK);
        System.out.println(nod);
        System.out.println(som);

        //Draw calendar
        for (int i = 1; i <= nod; i++) {
            int row = new Integer((i + som - 2) / 7);
            int column = (i + som - 2) % 7;
            jTable1.setValueAt(i, row, column);
        }

        //if (currentMonth == realMonth && currentYear == realYear){ //Today
           // jTable1.getSelectedRow(), jTable1.getSelectedColumn().setSelectionForeground(new Color(0, 220, 255))
	                 //   setBackground(new Color(0, 220, 255));}
       //Apply renderers
	//jTable1.setDefaultRenderer(jTable1.getColumnClass(0), new tblCalendarRenderer());

    }

    class tblCalendarRenderer extends DefaultTableCellRenderer{
	        public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
	            super.getTableCellRendererComponent(table, value, selected, focused, row, column);
                    if (column == 0){ //Week-end
	                setBackground(new Color(255, 220, 220));
	            }
	            else{ //Week
	                setBackground(new Color(255, 255, 255));
	            }
	            if (value != null){
	                if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && currentYear == realYear){ //Today
	                    setBackground(new Color(0, 220, 255));

                        //System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(), jTable1.getSelectedColumn()));
	                }
	            }
	            setBorder(null);
	            setForeground(Color.black);
	            return this;
	        }
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel background;
    public static javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblMonth;
    private javax.swing.JLabel lblNext;
    private javax.swing.JLabel lblNext1;
    private javax.swing.JLabel lblPrev;
    private javax.swing.JLabel lblPrev1;
    private javax.swing.JLabel lblYear;
    private javax.swing.JComboBox yearComboBox;
    // End of variables declaration

    
	@Override
	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String command = evt.getActionCommand();
		if (command.equals("Store New Event")) 
	   {
			this.dispose();
			if(combineDate==null)
				enterEvent.dateButton.setText("Click date");
			else
			    enterEvent.dateButton.setText(combineDate);
			firstPage.newEvent.setVisible(true);
			firstPage.callFromHere=false;
	   }
		if (command.equals("Back"))
		{
			if(firstPage.callFromHere)
			{
			this.dispose();
			firstPage.first_page.setVisible(true);
			firstPage.first_page.setSize(userMainPage.screensize.width,userMainPage.screensize.height);
			}
			else if (!firstPage.callFromHere)
			{
				this.dispose();
			}
		}
		if (command.equals("OK"))
		{
			this.dispose();
			listAllEvent.setDateBtn.setText(combineDate);
		}
	
	}
}



